<?php

namespace App\Admin\Controllers;


use App\Models\DevBookInfo;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;
use Illuminate\Http\Request;
use  App\Api\Controllers\DepotController;

class DevBookInfoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){

            $content->header('数据');
            $content->description('成都理工数据');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('数据');
            $content->description('成都理工数据');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('数据');
            $content->description('成都理工数据');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(DevBookInfo::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                // prepend一个操作
                $actions->disableDelete();
            });
            $grid->model()->where('type', 0);
            $grid->id('ID');
            $grid->title('书名')->display(function ($data){
                return $data.' '.$this->author;
            });
            $grid->publisher('出版社');
            $grid->author('作者');
            $grid->isbn('isbn');
            $grid->status()->sortable();
            $grid->type('内部教材');


        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(DevBookInfo::class, function (Form $form) {
            $form->text('title','书名');
            $form->text('author','作者');
            $form->text('publisher','出版社');
            $form->text('isbn','isbn');
            $form->radio('type','内部教材')->options(['0' => '不是', '1'=> '是'])->default('0');
            $form->saved(function (Form $form) {
                if(!$form->model()->isbn){
                    $error = new MessageBag([
                        'title'   => '错误提示',
                        'message' => 'isbn缺失',
                    ]);
                    return back()->with(compact('error'));
                }
                $depot=new DepotController();
                $goods=$depot->getGoodsInfo($form->model()->isbn);
                if(!$goods){
                    $error = new MessageBag([
                        'title'   => '错误提示',
                        'message' => '未查询到该书籍',
                    ]);
                    return redirect('/admin/goods/create');

                }

                $devBook=DevBookInfo::find($form->model()->id);
                $devBook->status=1;
                if($devBook->save()){
                    $success = new MessageBag([
                        'title'   => '成功',
                        'message' => '更新本条记录成功',
                    ]);

                    return redirect('/admin/devBookInfo?page=5');
                }else{

                    $error = new MessageBag([
                        'title'   => '错误提示',
                        'message' => '更新本条记录失败',
                    ]);
                    return redirect('/admin/goods/create');
                }

            });

        });
    }
}
